package com.controller;

import java.io.IOException;
import java.io.PrintWriter;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.bean.Change;
import com.bean.Client;
import com.bean.Housepayment;
import com.bean.Marketing;
import com.bean.Room;
import com.service.ChangeService;
import com.service.HousepaymentService;
import com.service.MarketingService;
import com.service.RoomService;

@Controller
@RequestMapping("change")
public class ChangeController {

	
	@Autowired
	private RoomService roomservice;
	
	
	@Autowired
	private ChangeService cs;
	
	
	@Autowired
	private HousepaymentService hs;
	
	@Autowired
	private MarketingService ms;
	
	
	@RequestMapping("/huanfang.do")
    public String huanfang(Integer hid,Integer roomid,String rr){
		
		System.out.println("hid="+hid);
		System.out.println("rr"+rr);
		
		Housepayment housepayment = hs.selectByPrimaryKey(hid);
		Change bean =new Change();
		if(housepayment.getBargainid()!=null){
			bean.setBid(housepayment.getBargainid());}
			
		
		if(housepayment.getIndentid()!=null){
		bean.setIid(housepayment.getIndentid());}
		
		bean.setRoomid(housepayment.getRoomid());
		bean.setRoom(rr);
		bean.setNewroomid(roomid);
		//通过机会表查找客户id
		Integer maketigid = housepayment.getMaketigid();
		Marketing marketing = ms.selectByPrimaryKey(maketigid);	
		bean.setCid(marketing.getClientid());
		System.out.println("uid"+housepayment.getUserid());
		System.out.println("uwwid"+housepayment.getId());
		bean.setHid(hid);
		bean.setUid(housepayment.getUserid());
		bean.setState("换房审核中");
		bean.setType(1);
		cs.insert(bean);
		
		//改变新选房间状态
		Map<String,Object> map=new HashMap<String,Object>();
		map.put("id",roomid);
		map.put("state","换房中");
		int i = roomservice.updateByidstate(map);
		System.out.println("改变新选房间状态"+i);
		
		//改变合同表状态
		Map<String,Object> map1=new HashMap<String,Object>();
		map1.put("id",hid);
		map1.put("state","换房审核中");
		int j = hs.updateByPrimaryKeystate(map1);
		System.out.println("改变合同表状态"+j);
		
		return "redirect:/housepayment/housepayment.do";
    }
	
	
	
	@RequestMapping("/list.do")
    public String list(HttpServletRequest re,Model model){
		Integer uid = (Integer) re.getSession().getAttribute("uid");
		Integer rid = (Integer) re.getSession().getAttribute("rid");
		
		List<Change> list=new ArrayList<Change>();
		List<Change> list1=new ArrayList<Change>();
		
		System.out.println("size++list1.size()");
		
			list=cs.selectAll();
		 list1 = cs.selectAll1();
	System.out.println("size1+"+list1.size());
	System.out.println("size+"+list.size());
	
		model.addAttribute("list", list);
		model.addAttribute("list1", list1);
		
		return "housepayment/shenhe";
		
		
	}
	
	
	@RequestMapping("/tongguo.do")
    public String tongguo(Integer id,Model model){
		
		Change change = cs.selectByid(id);
		Integer hid = change.getHid();
		Housepayment housepayment = hs.selectByPrimaryKey(hid);
		//判断Change类型
		Integer type = change.getType();
		//换房审核 通过  原有房间状态改变  （1）将新房间状态改变更名审核通过（2） 改变房款合同状态
		if(type==1){
			
			Integer newroomid = change.getNewroomid();
			if(housepayment.getBargainid()!=null){
		  	
		  	Map<String,Object> map=new HashMap<String, Object>();
		  	map.put("id", newroomid);
			map.put("state","签约");
		  	roomservice.updateByidstate(map);}else{
		  		
		  		
			  	Map<String,Object> map1=new HashMap<String, Object>();
			  	map1.put("id", newroomid);
				map1.put("state","认购");
			  	roomservice.updateByidstate(map1);
		  		
		  	}
			//原有房间状态改变
			Integer roomid = change.getRoomid();
			Map<String,Object> map2=new HashMap<String, Object>();
		  	map2.put("id", roomid);
			map2.put("state","已开盘");
			roomservice.updateByidstate(map2);
			
		  	//改房款信息
			//已收金额
			Float accomplishmoney = housepayment.getAccomplishmoney();
		    System.out.println("已收金额="+accomplishmoney);
		    Room room = roomservice.selectByPrimaryKey(newroomid);
		    
		    //应收金额
		    Float roomtotal = room.getRoomtotal();
		    
		    //更改房款信息的状态  房间id 应还金额 已还金额 未还金额
		    housepayment.setState("换房申请通过，请继续还款"); 
		    housepayment.setRoomid(newroomid);
		    housepayment.setReceivablemoney(roomtotal);
		    housepayment.setAccomplishmoney(accomplishmoney);
		    housepayment.setUncollectedmoney((roomtotal-accomplishmoney));
		    int i = hs.updateByPrimaryKeySelective(housepayment);
		    System.out.println("更改房款信息的状态  房间id 应还金额 已还金额 未还金额+"+i);
		    
		}
		if(type==2){
			//改房款状态 落户名
			String newcname = change.getNewcname();
			 housepayment.setState("更名申请通过，请继续还款");
			 housepayment.setFname(newcname);
			int i = hs.updateByPrimaryKeySelective(housepayment);
			System.out.println("改房款状态 落户名+"+i);
		    
			
		}
		
		//改审核表状态
		Map<String,Object> map2=new HashMap<String, Object>();
	  	map2.put("id", id);
	  	map2.put("state","通过");
		int i = cs.updateState(map2);
		
		System.out.println("改审核表状态+"+i);
		
		
		return "redirect:list.do";
		
		
	}
	
	
	
	@RequestMapping("/weitongguo.do")
    public String weitongguo(Integer id,Model model){
		
		System.out.println("iaaaaaad+"+id);
		Change change = cs.selectByid(id);
		Integer hid = change.getHid();
		
		//判断Change类型
		Integer type = change.getType();
		//换房审核 未通过  原有房间状态不变  （1）将新房间状态改为已开盘（2） 改变房款合同状态
		if(type==1){
		  	Integer newroomid = change.getNewroomid();
		  	Map<String,Object> map=new HashMap<String, Object>();
		  	map.put("id", newroomid);
			map.put("state","已开盘");
		  	roomservice.updateByidstate(map);
		  	//改房款状态
		
		  	Map<String,Object> map1=new HashMap<String, Object>();
		  	map1.put("id", hid);
			map1.put("state","换房申请未通过，请继续还款");
			hs.updateByPrimaryKeystate(map1);
			
		
		}
		if(type==2){
			//改房款状态
		  	
		  	Map<String,Object> map2=new HashMap<String, Object>();
		  	map2.put("id", hid);
			map2.put("state","更名申请未通过，请继续还款");
			hs.updateByPrimaryKeystate(map2);
			
			
		}
		
		//改审核表状态
		Map<String,Object> map2=new HashMap<String, Object>();
	  	map2.put("id", id);
	  	map2.put("state","未通过");
		int i = cs.updateState(map2);
		
		System.out.println("改审核表状态+"+i);
		return "redirect:list.do";
		
		
	}
	
	
	
	
	
	@RequestMapping("/gengming.do")
    public String gengming(Integer hid,String fname,String room,String rr){
		System.out.println("hid="+hid);
		System.out.println("rr"+fname);
		
		Housepayment housepayment = hs.selectByPrimaryKey(hid);
		Change bean =new Change();
		if(housepayment.getBargainid()!=null){
			bean.setBid(housepayment.getBargainid());}
			
		
		if(housepayment.getIndentid()!=null){
		bean.setIid(housepayment.getIndentid());}
		
	
		//通过机会表查找客户id
		Integer maketigid = housepayment.getMaketigid();
		Marketing marketing = ms.selectByPrimaryKey(maketigid);	
		bean.setCid(marketing.getClientid());
		System.out.println("uid"+housepayment.getUserid());
		System.out.println("uwwid"+housepayment.getId());
		bean.setHid(hid);
		bean.setRoomid(housepayment.getRoomid());
		bean.setUid(housepayment.getUserid());
		bean.setState("更名审核中");
		bean.setNewcname(fname);
		bean.setType(2);
		int i = cs.insert(bean);
		System.out.println("gengming"+i);
		
		//改变合同表状态
		Map<String,Object> map1=new HashMap<String,Object>();
		map1.put("id",hid);
		map1.put("state","更名审核中");
		int j = hs.updateByPrimaryKeystate(map1);
		System.out.println("改变合同表状态"+j);
		
		return "redirect:/housepayment/housepayment.do";
    }
	
	
	
	@RequestMapping("/toajax.do")
	public void toajax(Integer roomid,PrintWriter out,HttpServletRequest re) throws IOException{
	
		
		Room room = roomservice.selectByPrimaryKey(roomid);
		 
		Float roomtotal = room.getRoomtotal();
	 out.print(roomtotal);
				
		
	}
	
	
}
